//microcommand define finduser
inject(["database"])
signature("<user:user>")
action(async ({ session }, user) => {
  if (!user) return session.i18n("admin.user-expected");
  if (!user.startsWith(session.platform + ":")) return session.i18n("lnnbot.admin.invalid-user-platform")
  const prefix = `whatlang_members_${session.platform}:`
  const userId = user.slice(session.platform.length + 1)
  const cacheRecords = await ctx.database.get("cache", {
    table: new RegExp("^" + escapeRegExp(prefix)),
    key: userId
  }, ["table"])
  if (!cacheRecords.length) return session.i18n(".not-found", await session.bot.getUser(userId))
  return [
    ...session.i18n(".result-header", await session.bot.getUser(userId)),
    ...await Promise.all(cacheRecords.map(async ({ table }) => {
      const guildId = table.slice(prefix.length);
      const guild = await session.bot.getGuild(guildId)
      return h("p", guild.name)
    })),
  ]
})
locale("zh-CN", {
  description: "查找指定用户近期发言的群",
  arguments: { user: "用户" },
  messages: {
    "user-expected": "缺少用户…",
    "result-header": "“{name}”近期发言的群有：",
    "not-found": "未找到“{name}”近期的发言记录…",
  },
})